﻿using UnityEngine;

namespace StarForce
{

    public class AfterglowEffect : MonoBehaviour {

        private Material[] mats;
        private Vector3 prePosition;
        private Vector3 curPosition;
        private float deltaTime;

        // Use this for initialization
        void Start()
        {
            prePosition = curPosition = transform.position;
            Renderer[] renderers = transform.GetComponentsInChildren<Renderer>();
            mats = new Material[renderers.Length];
            for (int i = 0; i < renderers.Length; i++)
            {
                Renderer renderer = renderers[i];
                mats[i] = renderer.sharedMaterial;
            }
        }

        // Update is called once per frame
        void Update()
        {
            curPosition = transform.position;

            if (curPosition == prePosition)
            {
                deltaTime = 0;
                return;
            }

            deltaTime += Time.deltaTime;
            prePosition = Vector3.Lerp(prePosition,curPosition,deltaTime);

            Vector3 direction = prePosition- curPosition;
            for (int i = 0; i < mats.Length; i++)
            {
                mats[i].SetVector("_Direction", new Vector4(direction.x, direction.y, direction.z, mats[i].GetVector("_Direction").w));
            }
        }
    }


}
